from flask import jsonify
from elasticsearch import Elasticsearch

es = Elasticsearch()


def test():
    body = {
        "query": {
            "match_all": {}
        }
    }
    # es.create(index="baven", doc_type="type_1", id=4, body={"name": "python5", "addr": "深圳龙岗"})
    body1 = {"name": "python", "addr": "深圳龙华", "man": {"number": 50, "year": 35, "from": ['shanxi', "henan"]}}
    # es.index(index="baven", doc_type="type_1", body={"name": "python5", "addr": "深圳龙岗"})
    # es.delete(index="baven", doc_type="type_1", id=1)
    # es.delete_by_query(index="baven", doc_type="type_1", body=body)
    # es.update(index="baven", doc_type="type_1", id='0YO4w2kBEYtVu8YDadwS',  body={"doc": {"name": "python5", "addr": "深圳宝安"}})
    # es.indices.delete(index='baven2')
    return jsonify({'msg': "创建ok"})


def select_test():
    body = {
        "query": {
            "match_all": {}
        }
    }
    result = es.search(index="baven", doc_type="type_1", body=body)
    # result = es.get(index="baven", doc_type="type_1", id='0YO4w2kBEYtVu8YDadwS')
    data = result["hits"]["hits"]
    return jsonify({"msg": "查询ok", "data": data, "count":  result["hits"]["total"]})